home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!marnold
- From: marnold@netcom.com (Matt Arnold)
- Subject: Re: Simple elastic list in C++...
- Message-ID: <marnoldDpJ98F.3q5@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- References: <4ka4d5$cia@park.interport.net>
- Date: Mon, 8 Apr 1996 07:38:39 GMT
- Sender: marnold@netcom12.netcom.com
-
- scamhi@interport.net (Steven Camhi) writes:
-
- >As a programmer old to C, new to C++, I have seen no 'simple' answer to the
- >following situation. In most cases, the easiest, simplest way (for me, that
- >is), to create a growable list in straight ANSI C, has been the following:
- [snip]
- >What is *the best* way of doing dynamically growable lists in C++, without a
- >headache?
-
- Uh, use STL?
-
- >Perhaps there is no *best* way, what is the way to approach the
- >problem of manufacturing a simple list class. One way I have seen is the use
- >of the STL vector classes; thats been my frame of reference.
-
- There's not many ways to do it other than the how the vector class in STL
- is implemented. That's the basic model when the base implementation is an
- array (contiguous items in memory). You resize the block holding the array,
- move items up and down, etc..
-
- The next technique would be linked list. Again, not to many variations
- there. The STL implementation (the list class) is as good as any other.
- The only real choice is whether you use a singly- or doubly-linked list.
- STL uses a doubly-linked list, I believe.
-
- >I've seen some examples on the net use realloc(), and I've heard thats a major
- >no-no! Whats the right way?
-
- Mixing C++ new/delete with realloc() is the no-no. Using reallac() itself
- is OK---although it's only meant to be used with memory returned by malloc(),
- of course. realloc() and malloc() are part of the standard C library. All
- C++ programs are C programs---there's nothing stopping you from using the
- standard C memory allocation routines in a C++ program if you want to. Just
- #include "alloc.h" and code away. You just have to be careful not to use
- operator::delete() on memory you got from malloc(), etc..
-
- This is an example of the "no-no's" you've heard of...
-
- Object* p = new Object();
- realloc(p); // no, NO!
-
- There's no garantee that new() and realloc() use the same underlying
- memory manager. Passing realloc() an address obtained from malloc()
- is just like passing it garbage. You're likely to corrupt new's heap,
- the C library's heap, or both.
-
- This is fine...
-
- MY_DATA* p = (MY_DATA*)malloc(sizeof(MY_DATA));
- p = (MY_DATA*)realloc(p, sizeof(MY_DATA) + 42);
-
-
- Question: If you need lists, what's wrong with using STL? Taking care of
- these kinds of programming issues in a standardized way is what something
- like STL is all about. Why not use it?
-
- 'Course, I have found that trying to create container classes in C++ to
- be a very good learning experience. I encourage you to persue that if
- this is what you are after. However, for the people who created STL at
- HP, this was their *job*, or at least a very important "extra-curricular"
- activity. They've done a very good job with it, I think.
-
- For learning STL, I've found the book "The C++ Programmer's Guide to the
- Standard Template Library" from IDG books to be quite good. It comes
- with the HP version of STL.
-
- Regards,
- -------------------------------------------------------------------------
- Matt Arnold | | ||| | |||| | | | || ||
- marnold@netcom.com | | ||| | |||| | | | || ||
- Boston, MA | 0 | ||| | |||| | | | || ||
- 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- -------------------------------------------------------------------------
-